<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../dashboard/mixins/resize'
import {param} from "../../utils";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    finished: {
      type: String
    },
    unFinished: {
      type: String
    }
  },
  data() {
    return {
      chart: null,
      value1: 0,
      value2: 0
    }
  },
  watch: {
    finished: {
      handler(newVal) {
        this.value1 = parseInt(newVal)
        this.$nextTick(() => {
          this.initChart()
        })
      },
      immediate: true
    },
    unFinished: {
      handler(newVal) {
        this.value2 = parseInt(newVal)
        this.$nextTick(() => {
          this.initChart()
        })
      },
      immediate: true
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)

      this.chart.setOption({
        title: {
          text: '缴费户数比例',
          x: 'center',
          y: 'top'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        grid: {
          top: 40,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['缴费户数', '未交费户数']
        },
        series: [
          {
            name: '缴费户数比例',
            type: 'pie',
            roseType: 'radius',
            radius: '50%',
            center: ['50%', '50%'],
            data: [
              { value: this.value1, name: '缴费户数' },
              { value: this.value2, name: '未交费户数' }
            ], animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>

